﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FACUI.BasicClass
{
    /// <summary>
    /// 绝缘节
    /// </summary>
    public class JYJ : Sharp

    {
        private Point pointj;
        private static Point temp1;
        
        public JYJ(MapData mapData, Point pt, int d, string subType) : base(mapData, pt, d)
        {
            base.digitList.Add(new Digit("正常", "CGQ_T", 1));
            base.digitList.Add(new Digit("故障", "CGQ_F", 1));
            base.changeStep = 1;
            base.text =string.Format("{0}.{1}",pt.X,pt.Y);
            base.sharpType = "sbJYJ";
            this.changeSubType(subType);
        }

        public override void addSize(int r)
        {
        }

        public override void addSize(int d, Point oldPoint, int oldWidth, int oldHeight, int width, int height)
        {
        }

        public override bool changeSubType(string newSubType)
        {
            if (int.Parse(newSubType)<0 || int.Parse(newSubType)>2)
            {
                return false;
            }
            base.sizeDefine = new Size(1, 1);
            base.subType = newSubType;
            this.resetSize();
            return true;
        }

        public override void checkLine(int[][][] array4checkLine)
        {
        }

        public override void createDigit(string kglDesc)
        {
        }

        public override double distance(Point p, Size r)
        {
            double num = Math.Pow((p.X - (((((double) (this.rectReal.Right + this.rectReal.Left)) / 2.0) * r.Width) / 2.0)) / 2.0, 2.0);
            double num2 = Math.Pow((p.Y - (((((double) (this.rectReal.Bottom + this.rectReal.Top)) / 2.0) * r.Height) / 2.0)) / 2.0, 2.0);
            int direction = base.direction;
            return (Math.Sqrt(num + num2) / 2.0);
        }

        public override void draw(Graphics g, MapSet ct, Pen usePen)
        {
            if (base.inShowArea())
            {
                this.resetSize();
                Rectangle rectangle = new Rectangle((((this.rectReal.Left * ct.gridsize.Width) / 2) + ct.gridsize.Width) + ct.offset.X, (((this.rectReal.Top * ct.gridsize.Height) / 2) + ct.gridsize.Height) + ct.offset.Y, (this.rectReal.Width * ct.gridsize.Width) / 2, (this.rectReal.Height * ct.gridsize.Height) / 2);
                StringFormat format = new StringFormat(StringFormatFlags.NoClip) {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                };
                base.linePen.Color = base.lineColor;
                base.linePen.Width = ct.lineWidth;
                this.pointj = new Point(base.pointX + 1, base.pointY + 1);
                temp1 = base.grid2pix(new Point(base.pointX, base.pointY + 1), ct.gridsize, ct.offset);

                PointF pointO = new PointF(temp1.X+ct.gridsize.Width,temp1.Y);
                double santa = this.direction * Math.PI / 4d;
                float baseWidth=ct.gridsize.Width;
                float baseHeight=ct.gridsize.Height;

                Pen pen = (usePen != null) ? usePen : base.linePen;
                pen.Width = (ct.gridsize.Width >= 8) ? pen.Width : 0f;
                pen.Color = (ct.gridsize.Width >= 6) ? pen.Color : Color.Black;
                if (MapData.开始打印)
                {
                    pen = new Pen(Color.Black, pen.Width);
                }
                if (int.Parse(base.subType) == 0 || int.Parse(base.subType) == 1)
                {
                    if (int.Parse(base.subType) == 1)
                    {
                        g.DrawEllipse(new Pen(Color.Red), pointO.X-0.5f*baseWidth, pointO.Y-0.5f*baseHeight, (float) baseWidth, (float) baseHeight);
                    }
                    g.DrawLine(pen, pointO.X + (float)Math.Round(Math.Cos(santa), 4) * 0.5f * baseWidth,
                        pointO.Y - (float)Math.Round(Math.Sin(santa), 4) * 0.5f * baseHeight,

                        pointO.X + (float)Math.Round(Math.Cos(santa+Math.PI), 4) * 0.5f * baseWidth,
                        pointO.Y - (float)Math.Round(Math.Sin(santa+Math.PI), 4) * 0.5f * baseHeight);
                }
                else if (int.Parse(base.subType) == 2)
                {
                    PointF pointLeft = new PointF(pointO.X + (float)Math.Round(Math.Cos(santa), 4) * 0.5f * baseWidth,
                        pointO.Y - (float)Math.Round(Math.Sin(santa), 4) * 0.5f * baseHeight);
                    PointF pointRight = new PointF(pointO.X + (float)Math.Round(Math.Cos(santa + Math.PI), 4) * 0.5f * baseWidth,
                        pointO.Y - (float)Math.Round(Math.Sin(santa + Math.PI), 4) * 0.5f * baseHeight);

                    g.FillEllipse(new SolidBrush(pen.Color), pointLeft.X - 0.5f * baseWidth, pointLeft.Y - 0.5f * baseHeight, baseWidth, baseHeight);
                    g.FillEllipse(new SolidBrush(pen.Color), pointRight.X - 0.5f * baseWidth, pointRight.Y - 0.5f * baseHeight, baseWidth, baseHeight);
                    g.DrawLine(pen, pointO.X + (float)Math.Round(Math.Cos(santa), 4) * baseWidth,
                        pointO.Y - (float)Math.Round(Math.Sin(santa), 4) * baseHeight,

                        pointO.X + (float)Math.Round(Math.Cos(santa+Math.PI), 4) * baseWidth,
                        pointO.Y - (float)Math.Round(Math.Sin(santa+Math.PI), 4) * baseHeight);
                }
                if (!(base.isHideTxt || (((int) (base.txtSize * ((ct.gridsize.Width * 1.0) / 10.0))) < 4)))
                {
                    g.DrawString(base.text, new Font(base.txtFont, (float)((int)(base.txtSize * ((ct.gridsize.Width * 1.0) / 10.0))), FontStyle.Regular), new SolidBrush(MapData.开始打印 ? Color.Black : base.txtColor), rectangle, format);
                }
            }
        }

        public override Point getPoint(char c)
        {
            Point point = new Point(-1, -1);
            switch (c)
            {
                case 'J':
                case 'P':
                case 'C':
                    return this.pointj;
            }
            return point;
        }

        public override bool hasHotPoint(int d)
        {
            return false;
        }

        public override bool hasPoint(Point pt)
        {
            return pt.Equals(this.pointj);
        }

        public override void onSpaceChar()
        {
            base.direction = (base.direction + base.changeStep) % 4;
            base.sizeDefine = new Size(1, 1);
            this.resetSize();
        }

        public override void resetSize()
        {
            this.rectReal.Location = new Point((base.pointX * 2) , (base.pointY * 2) );
            this.rectReal.Width = 4;
            this.rectReal.Height = 4;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("");
            builder.Append(StringUtil.TrimAndPadRight4ASCII(base.text, 1)).Append("绝缘节");
            switch (int.Parse(base.subType))
            {
                case 0:
                    builder.Append("->普通绝缘节:");
                    break;
                case 1:
                    builder.Append("->超限绝缘节:");
                    break;
                case 2:
                    builder.Append("->传感器绝缘节:");
                    break;
            }
            builder.Append(" 坐标(").Append(base.pointX).Append(',').Append(base.pointY).Append(')');
            return builder.ToString();
        }

        public override string toStringKgl()
        {
            return "";
        }

        public override string toStringXiangshu()
        {
            return string.Concat(new object[] { 
                base.text, ",", base.sharpType, ",", base.subType, ",", (base.lineColor.R + (base.lineColor.G << 8)) + (base.lineColor.B << 0x10), ",", base.direction, ",", base.pointX, ",", base.pointY, ",", base.sizeDefineWidth, ",", 
                base.sizeDefineHeight, ",", base.txtSize, ",", base.txtColor.ToArgb() & 0xffffff, ",", base.txtFont, ",", base.isHideTxt ? "1" : "0"
             });
        }

        public override void xuanzhuan180(Point center)
        {
            base.xuanzhuan180(center);
            switch (base.direction)
            {
                case 1:
                    base.direction = 3;
                    break;

                case 3:
                    base.direction = 1;
                    break;
            }
            base.pointX -= 2;
            base.pointY -= 2;
        }
    }
}

